import { request } from '@/utils/request.js'
import Vue from "vue";
let isFirstAppRun = true;
let isGettingFromService = false;
let mallConfig = null;
let cacheKey = '_APP_CONFIG';
let resolveList = [];
let rejectList = [];
let tath = Vue.prototype
const getConfigFormServer = (resolve, reject) => {
    if (resolve) {
        resolveList.push(resolve);
    }
    if (reject) {
        rejectList.push(reject);
    }
    if (isGettingFromService) {
        return;
    }
    isGettingFromService = true;
    request({
        url: 'api/index/config',
    }).then(response => {
        isGettingFromService = false;
        if (response.code === 0) {
            mallConfig = response.data;
            uni.setStorageSync(cacheKey, mallConfig);
            // 手动缓存清除
            // response.data.mall.setting.clear_login_status_version = 4
            console.log(response.data.mall.setting.clear_login_status_version, '拿到清除缓存的值')
            let status_version = response.data.mall.setting.clear_login_status_version
            const value = uni.getStorageSync('status_version');
            if (value) {
                if (status_version > value) {
                    uni.setStorageSync('lgoin_version', status_version);
                    // #ifdef H5
                    uni.removeStorageSync("status_version");
                    uni.removeStorageSync("time_stamp");
                    uni.removeStorageSync('Satore');
                    uni.removeStorageSync('_USER_ACCESS_TOKEN_1_mobile');
                    uni.removeStorageSync('set_kcUserInfo');
                    setTimeout(() => {
                        uni.$emit('update', { msg: '注销刷新' })
                    }, 600)
                    // #endif
                }
            } else {
                uni.setStorageSync('status_version', status_version);
            }
            for (let i in resolveList) {
                resolveList[i](mallConfig);
            }
            resolveList = [];
        } else {
            for (let i in rejectList) {
                rejectList[i](response.msg);
            }
            rejectList = [];
        }
    }).catch(error => {
        isGettingFromService = false;
        for (let i in rejectList) {
            rejectList[i](error.msg);
        }
        rejectList = [];
    });
};

export default {
    getConfig(options) {
        return new Promise((resolve, reject) => {            
            if (mallConfig) {
                return resolve(mallConfig);
            }
            mallConfig = uni.getStorageSync(cacheKey);
            if (mallConfig) {
                if (isFirstAppRun) {
                    isFirstAppRun = false;
                    getConfigFormServer();
                }
                return resolve(mallConfig);
            } else {
                getConfigFormServer(resolve, reject);
            }
        });
    },
    resetConfig() {
        mallConfig = null;
        uni.removeStorageSync(cacheKey);
        isGettingFromService = false;
    }
};
